//
// Copyright (c) 2014, 保留所有权利。
//
// FlagLog.java
//
// 作    者：王红燕
// 当前版本：1.0
// 2014 上午10:52:33
//
// 
//
package com.vanke.agent.common;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.Set;

import org.apache.log4j.Logger;

import com.vanke.agent.ADataSource;
import com.vanke.agent.bean.FlagLogBean;
import com.vanke.agent.common.log.LoggerUtil;

/**
 * 
 */
public class FlagLog {
	static Logger log = LoggerUtil.getLoggerByName("flag_log");
	static DateFormat tableFormat = new SimpleDateFormat("yyyyMM");
	static Set<String> flagTables = new HashSet<String>();

	public static void saveFlagLog(FlagLogBean bean) {
		if (bean == null) {
			return;
		}
		if (bean.getFlagTime() == null) {
			log.error("缺少必须字段[FlagTime][" + bean + "]");
			return;
		}
		Connection bizCon = ADataSource.getConnection(ADataSource.SCHEMA_BIZ);
		// "FlagGUID,CstGUID,OriCstGUID,Appid,Flag,SubStatus,FlagTime,ProjCode,ProjGroup,RoomGUID,UserGUID,PrmAgent,RecommendGUID,Comment,CreateTime"
		String fields = "CstGUID,OriCstGUID,Appid,Flag,SubStatus,FlagTime,ProjCode,RoomGUID,UserGUID,PrmAgent,RecommendGUID,Comment,CreateTime,RefGUID";
		String table = retrieveTable(bizCon, bean);
		String sql = CstUtil.formatInsertSql(table, fields);
		PreparedStatement pstmt = null;
		try {
			Timestamp flagTime = new Timestamp(bean.getFlagTime().getTime());
			Timestamp curTime = new Timestamp(System.currentTimeMillis());
			pstmt = bizCon.prepareStatement(sql);
			pstmt.setLong(1, bean.getCstGUID()); 		// CstGUID,
			pstmt.setString(2, bean.getOriCstGUID());	// OriCstGUID,
			pstmt.setInt(3, bean.getAppid());			// Appid,
			pstmt.setInt(4, bean.getFlag());			// Flag,
			pstmt.setString(5, bean.getSubStatus());	// SubStatus,
			pstmt.setTimestamp(6, flagTime);			// FlagTime,
			pstmt.setString(7, bean.getProjCode());		// ProjCode,
			pstmt.setString(8, bean.getRoomGUID());		// RoomGUID,
			pstmt.setString(9, bean.getUserGUID());		// UserGUID,
			pstmt.setString(10, bean.getPrmAgent());	// PrmAgent,
			pstmt.setString(11, bean.getRecommendGUID());// RecommendGUID,
			pstmt.setString(12, bean.getComment());		// Comment,
			pstmt.setTimestamp(13, curTime);			// CreateTime
			pstmt.setString(14, bean.getRefGUID());
			pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("保存标签日志异常：" + e);
		} finally {
			ADataSource.close(null, pstmt, bizCon);
		}
	} //~saveFlagLog

	private static String retrieveTable(Connection con, FlagLogBean bean) {
		String table = "cst_Flag_" + tableFormat.format(bean.getFlagTime());
		if (flagTables.size() == 0 || !flagTables.contains(table)) {
			flagTables.addAll(Cache.loadTables(con, "cst_Flag_"));
		}
		if (!flagTables.contains(table)) {
			createTable(con, table);
			flagTables.add(table);
		}
		return table;
	} //~retrieveTable

	static void createTable(Connection con, String table) {
		String sql = "create table if not exists " + table
				+ " (FlagGUID bigint(64) AUTO_INCREMENT primary key, KEY `index_cstguid` (`CstGUID`),KEY `index_flagtime` (`FlagTime`),KEY `index_projcode` (`ProjCode`)) "
				+ " ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci select * from cst_Flag where 1=2 ";
		Statement stmt = null;
		try {
			stmt = con.createStatement();
			log.info("execute " + sql);
			stmt.execute(sql);
			log.info("create table[" + table + "] succes");

			/*sql = "ALTER TABLE " + table;
			sql += " ADD INDEX `index_cstguid` (`CstGUID`) ,";
			sql += " ADD INDEX `index_flagtime` (`FlagTime`) ,";
			sql += " ADD INDEX `index_projcode` (`ProjCode`) ";
			stmt.execute(sql);
			log.info("add index[" + table + "] succes");*/
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ADataSource.close(stmt);
		}
	} // ~createTable
} //~

